﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net;
using System.Reflection;
using System.Windows.Forms;

namespace 打开
{
    class Updater
    {
        //当前版本号
        private Version CurrentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

        //最新版本号
        public Version LatestVersion { get; private set; }

        //最新发布名称
        public string LatestReleaseName { get; private set; }

        //最新发布介绍
        public string LatestReleaseBody { get; private set; }

        //最新发布地址
        public string Url { get; set; }

        //更新可用
        public bool UpdaterEnable = false;

        public Updater(string url)
        {
            Url = url;
            check();
        }

        public Updater(string owner, string path)
        {
            Url = "https://git.oschina.net/api/v5/repos/" + owner + "/" + path + "/releases/latest";
            check();
        }
        
        //更新回执信息
        public struct JSON
        {
            public string id { get; set; }
            public string tag_name { get; set; }
            public string target_commitish { get; set; }
            public string name { get; set; }
            public string body { get; set; }
        }

        private string value;

        //检查更新
        public bool check()
        {

            try
            {

                WebClient MyWebClient = new WebClient();

                MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

                Byte[] pageData = MyWebClient.DownloadData(Url); //从指定网站下载数据

                //string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312，则使用这句            

                string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8，则使用这句

                //Console.WriteLine(pageHtml);//在控制台输入获取的内容

                value = pageHtml;

            }

            catch (WebException webEx)
            {

                Console.WriteLine(webEx.Message.ToString());
                return UpdaterEnable;
            }

            var release = JsonConvert.DeserializeObject<JSON>(value);

            LatestVersion = new Version(release.tag_name.Trim('v'));
            LatestReleaseName = release.name;
            LatestReleaseBody = release.body.Replace(@"\r\n", "\n");

            //比较最新版本和当前版本
            if (LatestVersion > CurrentVersion)
            {
#if DEBUG
                Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + "有新版可以更新");
#endif

                //更新可用为真
                UpdaterEnable = true;

                MessegeForm msf = new MessegeForm();
                msf.createForm("有新版可以更新", MessegeForm.ButtonKind.是否钮);
                if (msf.DialogResult == DialogResult.Yes)
                {
                    OpenUrl("https://git.oschina.net/XultimateX/quicklyopen/releases");
                }
            }
            else
            {
#if DEBUG
                Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + "无需更新");
#endif
            }
            return UpdaterEnable;
        }

        public void SetUrl(string str)
        {
            Url = str;
        }

        public void SetUrl(string owner, string path)
        {
            Url = "https://git.oschina.net/api/v5/repos/" + owner + "/" + path + "/releases/latest";
        }

        private void OpenUrl(string path)
        {
            System.Diagnostics.Process.Start(path);
        }

        private void OpenUrl(string owner, string path)
        {
            System.Diagnostics.Process.Start("https://git.oschina.net/" + owner + "/" + path + "/releases");
        }

    }
}
